var DocData={
    subclinicId:baseFn.getQueryString('subclinicId',true),
    ownerId:'',
    baseData:{},
    popMaster:function(data,ownerId){
        var _updata = {
            userStatus:0,
            subclinicId:data
        }
        $.get(baseUrl.base+baseUrl.sysuser+'list',_updata,function(res){
            if(res.code == 0){
                var _data = res.results.list
                var _txt=``;
                for(var h1=0;h1<_data.length;h1++){
                    _txt += `<div class="rows listelm">\
                                <div class="cols8">${_data[h1].userName}</div>\
                                <div class="cols10">${_data[h1].userPhone}</div>\
                                <div class="cols2" style=""><input type="radio" data-vals="${h1}" value="${h1}" class="checkBox`
                    if(_data[h1].userId == ownerId){
                        _txt+=` checkBox_checked`
                    }
                    _txt += `"/></div>\
                            </div>`;
                }
                $('.dialogelm.master .listcnt').html(_txt)
                $('.checkBox').off('click').on('click',function(){
                    var _parent = $(this).parents('.listcnt')
                    _parent.find('.checkBox')
                    if($(this).hasClass('checkBox_checked')){
                        $(this).removeClass('checkBox_checked');
                        DocData.ownerId=''
                    }else{
                        $(this).addClass('checkBox_checked');
                        $(this).parents('.listelm').siblings('.listelm').find('.checkBox').removeClass('checkBox_checked')
                        DocData.ownerId =_data[$(this).val()].userId
                    }
                })
            }
        },'json')
    },
    initBase:function(data){
        var that=this
        if(baseFn.notNull(that.subclinicId)){
            baseFn.$get({
                url:baseUrl.subclinic+'selectSubclinicInfo',
                data:{subclinicId:that.subclinicId},
                calFn:function(res){
                    var _data = res
                    if(baseFn.notNull(_data.subclinicName)){
                        $('.js_subclinicName').val(_data.subclinicName)
                    }else{
                        $('.js_subclinicName').prop('disabled',false)
                    }
                    if(baseFn.notNull(_data.subclinicLogo)){
                        $('.js_subclinicLogo').find('.imghead').attr('src',_data.subclinicLogo)
                        $('.js_subclinicLogo').find('.Js_imgUrl').val(_data.subclinicLogo)
                    }
                    if(baseFn.notNull(_data.subclinicStartTime)){
                        $('.js_subclinicStartTime').val(Number(_data.subclinicStartTime))
                    }
                    if(baseFn.notNull(_data.subclinicEndTime)){
                        $('.js_subclinicEndTime').val(Number(_data.subclinicEndTime))
                    }
                    if(baseFn.notNull(_data.subclinicAddress)){
                        $('.js_subclinicAddress').val(_data.subclinicAddress)
                    }
                    if(baseFn.notNull(_data.subclinicDescInfo)){
                        $('.uinfo[rel=info3]').find('textarea.js_infoTxt').val(_data.subclinicDescInfo)
                    }
                    if(baseFn.notNull(_data.subclinicHonorInfo)){
                        $('.uinfo[rel=info4]').find('textarea.js_infoTxt').val(_data.subclinicHonorInfo)
                    }
                    if(baseFn.notNull(_data.subclinicEnvironmentInfo)){
                        $('.uinfo[rel=info5]').find('textarea.js_infoTxt').val(_data.subclinicEnvironmentInfo)
                    }
                    if(baseFn.notNull(_data.subclinicDescImg)){
                        var _info3Arr=_data.subclinicDescImg.split(',')
                        var _$3 = $('.uinfo[rel=info3]').find('.imggroup'),_$btn=_$3.find('.addbtn')
                        for(var i=0;i<_info3Arr.length;i++){ 
                            _$btn.prev('.imgupcnt').find('.Js_preview').css({'background-image':'url('+_info3Arr[i]+')'}).addClass('added')
                            _$btn.prev('.imgupcnt').find('input.Js_imgUrl').val(_info3Arr[i])
                            baseFn.imgSize({
                                src: _info3Arr[i],
                                hFn: function(res) {
                                    res.dom.css({ 'background-size': 'auto 100%' })
                                },
                                wFn: function(res) {
                                    res.dom.css({ 'background-size': '100% auto' })
                                },
                                ratio: 1,
                                otherData: {
                                    dom:_$btn.prev('.imgupcnt').find('.Js_preview')
                                }
                            })
                            if(i != _info3Arr.length-1){
                                _$btn.trigger('click');
                            }
                        }
                    }
                    if(baseFn.notNull(_data.subclinicHonorImg)){
                        var _info4Arr=_data.subclinicHonorImg.split(',')
                        var _$4 = $('.uinfo[rel=info4]').find('.imggroup'),_$btn=_$4.find('.addbtn')
                        for(var i=0;i<_info4Arr.length;i++){ 
                            _$btn.prev('.imgupcnt').find('.Js_preview').css({'background-image':'url('+_info4Arr[i]+')'}).addClass('added')
                            _$btn.prev('.imgupcnt').find('input.Js_imgUrl').val(_info4Arr[i])
                            baseFn.imgSize({
                                src: _info4Arr[i],
                                hFn: function(res) {
                                    res.dom.css({ 'background-size': 'auto 100%' })
                                },
                                wFn: function(res) {
                                    res.dom.css({ 'background-size': '100% auto' })
                                },
                                ratio: 1,
                                otherData: {
                                    dom:_$btn.prev('.imgupcnt').find('.Js_preview')
                                }
                            })
                            if(i != _info4Arr.length-1){
                                _$btn.trigger('click');
                            }
                        }
                    }
                    if(baseFn.notNull(_data.subclinicEnvironmentImg)){
                        var _info5Arr=_data.subclinicEnvironmentImg.split(',')
                        var _$5 = $('.uinfo[rel=info5]').find('.imggroup'),_$btn=_$5.find('.addbtn')
                        for(var j=0;j<_info5Arr.length;j++){
                            _$btn.prev('.imgupcnt').find('.Js_preview').css({'background-image':'url('+_info5Arr[j]+')'}).addClass('added')
                            _$btn.prev('.imgupcnt').find('input.Js_imgUrl').val(_info5Arr[j])
                            baseFn.imgSize({
                                src: _info5Arr[j],
                                hFn: function(res) {
                                    res.dom.css({ 'background-size': 'auto 100%' })
                                },
                                wFn: function(res) {
                                    res.dom.css({ 'background-size': '100% auto' })
                                },
                                ratio: 1,
                                otherData: {
                                    dom:_$btn.prev('.imgupcnt').find('.Js_preview')
                                }
                            })
                            if(j != _info5Arr.length-1){
                                _$btn.trigger('click');
                            }
                        }
                        //$('uinfo[rel=info5]').find('textarea.js_infoTxt').val()
                    }
                }
            })
        }
    }

}
/*门店详情 */
//分店设置 tab切换
$('.bespeak_change_list_actve').on('click', function () {
    var thisIndex = $(this).index();
    $(this).addClass("active_bgc").siblings().removeClass("active_bgc");
    $('.js_tabcnt').eq(thisIndex).removeClass('hidden').siblings(".js_tabcnt").addClass('hidden');
})
DocData.initBase()

function upinfoimg(e){
    baseFn.imgUpload($(e).parents('form').attr('id'),function(res){
        $(e).siblings('.Js_preview').css({ "background-image": 'url(' + res.url + ')' })
        $(e).parents('form').siblings('input.Js_imgUrl').val(res.url)
        baseFn.imgSize({
            src: res.url,
            hFn: function(res) {
                res.dom.css({ 'background-size': 'auto 100%' })
            },
            wFn: function(res) {
                res.dom.css({ 'background-size': '100% auto' })
            },
            ratio: 1,
            otherData: {
                dom:$(e).siblings('.Js_preview')
            }
        })
        $(e).siblings('.Js_preview').addClass('added')
    })
}
function delimg(e){
    $(e).parents('.imgupcnt').remove()
}
$('.imggroup').on('click','.Js_preview',function(){
    $(this).siblings('input[name=img]').trigger('click')
})
$('.addbtn').on('click',function(){
    var $this=$(this),_type = $this.parents('.uinfo').attr('rel')
    var _imgupdom = '<div class="imgupcnt">\
            <form class="Js_uploadForm uploadform" method="post" enctype="multipart/form-data" id="'+_type+'_up_form'+(Number($this.data('step'))+1)+'">\
                <div class="Js_preview preview" style="background-image: url(&quot;&quot;);"><em onclick="delimg(this)"> - </em> </div>\
                <input type="file" name="img" onchange="upinfoimg(this)" style="display: none;">\
            </form>\
            <input type="hidden" class="Js_imgUrl" name="imgurl" value="" datatype="*" nullmsg="必须上传图片">\
        </div>'
        $this.before(_imgupdom)
        $this.data('step',Number($this.data('step'))+1)
})
$('.js_newsysuserinfo .edit_info_ok').on('click',function(){
    if(!baseFn.notNull(DocData.subclinicId)){
        baseFn.infoDialog('请选择门店',function(){
            baseFn.goPath('gotoStoreSetting')
        });
        return false
    }
    var _upDatas={
        subclinicId:DocData.subclinicId
    }
    var _url = baseUrl.base+baseUrl.subclinic+'updateSubclinic',_text='门店详情保存成功！'
    var _map={
        info3:'subclinicDescImg',
        info3Txt:'subclinicDescInfo',
        info4:'subclinicHonorImg',
        info4Txt:'subclinicHonorInfo',
        info5:'subclinicEnvironmentImg',
        info5Txt:'subclinicEnvironmentInfo'
    }
    
    $('.uinfo').each(function(v,n){
        var $this=$(n),_mapkey = $this.attr('rel')
        if(_map[_mapkey]+'Txt' &&　baseFn.notNull($this.find('.js_infoTxt').val())){
            _upDatas[_map[_mapkey+'Txt']]=$this.find('.js_infoTxt').val()
        }
        if(_map[_mapkey]){
            var _arr = []
            $this.find('.imgupcnt').each(function(s,m){
                if(baseFn.notNull($(m).find('input[name=imgurl]').val())){
                    _arr.push($(m).find('input[name=imgurl]').val())
                }
            })
            _upDatas[_map[_mapkey]]=_arr.join(',')
        }
        
    })
    $.post(_url,_upDatas,function(res){
        if(res.code == '0'){
            baseFn.dialog({
                title:"成功",
                content:_text+'是否返回门店列表？',
                ok:function(){
                    baseFn.goPath('gotoStoreSetting')
                },
                cancel:function(){}
            })
            // baseFn.infoDialog(_text,function(){
            //     window.location.href = baseUrl.gobase+'sysuser/gotoCtaffManagement'
            // })
        }
    },'json')
})
$('.js_newsysuser .edit_ok').on('click',function(){
    
    var _upDatas={
        subclinicId:DocData.subclinicId,
        subclinicAddress:$('.js_subclinicAddress').val(),
        subclinicStartTime:$('.js_subclinicStartTime').val(),
        subclinicEndTime:$('.js_subclinicEndTime').val(),
        subclinicLogo:$('.js_subclinicLogo input[name=imgurl]').val(),
    }
    var _url = baseUrl.base+baseUrl.subclinic+'updateSubclinic',_text='门店基本信息保存成功！',_txt='';
    if(!baseFn.notNull(DocData.subclinicId)){
        _txt = '请选择门店'  
    }
    if(!baseFn.notNull(_upDatas.subclinicAddress)){
        _txt = '请填写门店地址'
    }
    if(Number(_upDatas.subclinicStartTime) && Number(_upDatas.subclinicEndTime) && Number(_upDatas.subclinicStartTime) > Number(_upDatas.subclinicEndTime)){
        _txt = '开始时间不能大于结束时间'
    }
    if(!baseFn.notNull(_upDatas.subclinicLogo)){
        _txt = '请上传门店logo'
    }
    if(baseFn.notNull(_txt)){
        baseFn.infoDialog(_txt);
        return false
    }
    $.post(_url,_upDatas,function(res){
        if(res.code == '0'){
            baseFn.dialog({
                title:"成功",
                content:_text+'是否返回门店列表？',
                ok:function(){
                    baseFn.goPath('gotoStoreSetting')
                },
                cancel:function(){}
            })
            // baseFn.infoDialog(_text,function(){
            //     window.location.href = baseUrl.gobase+'sysuser/gotoCtaffManagement'
            // })
        }
    },'json')
})
$('.js_newsysuserinfo .edit_cancel').on('click',function(){
    baseFn.goPath('gotoStoreSetting')
})
$('.imghead').on('click',function(){
    $('#Js_previewImgNew').trigger('click')
})
function zcuploadImageCover(e){
    baseFn.imgUpload($(e).parents('form').attr('id'),function(res){
        $(e).siblings('.Js_preview').find('img').attr('src',res.url)
        $('.js_subclinicLogo input[name=imgurl]').val(res.url)
        // DocData.imgChange=true
    })
}
/*门店详情 */
/*门店列表 */
//分店列表
var subclinicFnData={
    ajUrl:baseUrl.base+baseUrl.subclinic+'selectSubclinicList',
    ajDataSrc:"results",
    ajData:{
        subclinicId:SessionData.powerSub?SessionData.powerSub:null
    },
    select:true,
    ajType:'get',
    sessionPageKey: "subclinicPage",
    scrollY:windowOffsetData.tableHeight(),
    autoWidth:false,
    columns:[
        { "data": "subclinicId",defaultContent:'',className:'imgpwrap',render:function(data,type,row, meta){
            var _img=baseUrl.tempHeadImg
            if(baseFn.notNull(row.subclinicLogo)){
                _img = row.subclinicLogo
            }
            if(row.subclinicLogo == null || row.subclinicLogo.length == 0){
                return '无'
            }else{
                return '<div class="datatable_img_div" data-vals="'+_img+'" style="background-image:url('+_img+')"></div>'
                // return '<img class="bespeak_change_img" src="'+row.subclinicLogo+'"/>';
            }
        }},
        { "data": "subclinicName",defaultContent:''},
        { "data": "subclinicAddress",defaultContent:'无'},
        { "data": "subclinicUserName",defaultContent:'无'},
        { "data": "userCount",defaultContent:'无'},
        { "data": "subclinicId",defaultContent:'无',className:"center",render:function(data,type,row, meta){
            var _txt = ''
            //权限管理 门店管理 - 58设置门店
            if(!baseFn.getPower(58)){
                _txt +=  '<a class="datatablebtn detail" href="javascript:void(0)" data-vals="'+data+'">设置门店</a>'
            }
            //权限管理 门店管理 - 30任命店长
            if(!baseFn.getPower(30)){
                _txt +=  '<a class="datatablebtn datatablebtnRed pop" href="javascript:void(0)" data-vals="'+data+'">任命店长</a>'
            }
            return _txt
        }}
    ],
    initComplete:function(){
        
    },
    page:function () {

    },
    drawCallback: function( settings ) {
        //图片初始化
        $('.datatable_img_div').each(function(ids,val){
            baseFn.imgSize({
                src: $(val).data('vals'),
                hFn: function(res) {
                    res.dom.css({'background-size':'auto 100%'})           
                },
                wFn: function(res) {
                    res.dom.css({'background-size':'100% auto'}) 
                },
                ratio: 1,
                otherData: {
                    dom:$(val)
                }
            })
        })
        $('.detail').off('click').on('click',function(){
            var _id = $(this).data('vals')
            baseFn.goPath('gotoStoreDetail',{subclinicId:_id})
        })
        $('.pop').off('click').on('click',function(){
            var _id = $(this).data('vals'), _tr = $(this).parents('tr'),_data = subclinic.row(_tr).data()
            DocData.subclinicId = _id
            DocData.popMaster(_id,_data.ownerId)
            baseFn.dialog({
                title:'请任命店长',
                content:$('.dialogelm.master'),
                height:'70vh',
                width:'50em',
                ok:function(){
                    if(DocData.ownerId != ''){
                        var _updata = {
                            subclinicId:DocData.subclinicId,
                            ownerId:DocData.ownerId
                        }
                        $.post(baseUrl.base+baseUrl.subclinic+'updateOwner',_updata,function(res){
                            if(res.code == 0){
                                baseFn.infoDialog('任命成功!')
                                DocData.ownerId =''
                                subclinic.ajax.reload(null,false)
                            }
                        },'json')
                    }else{
                        baseFn.dialog({
                            content:'请选择员工！',
                            ok:function(){  
                            }
                        })
                        return false
                    }
                    
                }
            })
        })
    }
}
//权限管理 门店管理 - 58设置门店 , 门店管理 - 30任命店长
if(baseFn.getPower(58) && baseFn.getPower(30)){
    subclinicFnData.columns.pop()
    $('.js_power_30_58').addClass('hidden').remove()
}
var subclinic = baseFn.rpDataTables('.subclinic_list',subclinicFnData)
/*门店列表 */
//表格自适应
window.onresize = function(){
    windowOffsetData.refresh();
    $(subclinic.table().body()).parents('.dataTables_scrollBody').css('max-height',windowOffsetData.tableHeight());
    subclinic.draw(false);
}



